﻿
// DAOTestView.cpp: CDAOTestView 类的实现
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "DAOTest.h"
#endif

#include "DAOTestDoc.h"
#include "DAOTestView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CDAOTestView

IMPLEMENT_DYNCREATE(CDAOTestView, CRowView)

BEGIN_MESSAGE_MAP(CDAOTestView, CRowView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CRowView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CRowView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CRowView::OnFilePrintPreview)
	ON_WM_SETFOCUS()
	ON_WM_MOUSEACTIVATE()
	ON_WM_TIMER()
END_MESSAGE_MAP()

// CDAOTestView 构造/析构

CDAOTestView::CDAOTestView() noexcept
{
	// TODO: 在此处添加构造代码
	m_pSet = NULL;
}

CDAOTestView::~CDAOTestView()
{
}

BOOL CDAOTestView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CRowView::PreCreateWindow(cs);
}

// CDAOTestView 绘图

void CDAOTestView::OnInitialUpdate()
{
	CRowView::OnInitialUpdate();
}


// CDAOTestView 打印

BOOL CDAOTestView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CDAOTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加额外的打印前进行的初始化过程
}

void CDAOTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加打印后进行的清理过程
}


// CDAOTestView 诊断

#ifdef _DEBUG
void CDAOTestView::AssertValid() const
{
	CRowView::AssertValid();
}

void CDAOTestView::Dump(CDumpContext& dc) const
{
	CRowView::Dump(dc);
}

CDAOTestDoc* CDAOTestView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDAOTestDoc)));
	return (CDAOTestDoc*)m_pDocument;
}
#endif //_DEBUG


// CDAOTestView 消息处理程序


void CDAOTestView::OnDrawRow(CDC* pDC, int nRowNo, int y, BOOL bSelected)
{
	int nX = 0;
	int i = 0;
	CDAOTestDoc* pDoc = GetDocument();
	if (m_pSet == NULL)
		return;
	if (nRowNo == 0)  // 绘制字段行,第0行
	{
		for (int i = 0; i < pDoc->m_nFileds; i++)
		{
			pDC->TextOutW(nX,y, pDoc->m_arrFiledName[i]);
			nX += pDoc->m_arrFiledSize[i] * m_nCharWidth;
		}
	}
	else
	{
		try 
		{
			m_pSet->SetAbsolutePosition(nRowNo -1);
			if (m_pSet->GetAbsolutePosition() == (nRowNo-1))
			{
				DrawDataRow(pDC,y);
			}
		}
		catch (CDaoException* e)
		{
			if (e->m_pErrorInfo->m_lErrorCode = 3167)
			{
				pDC->TextOutW(0, y, _T("**Record Delete**"));
			}
			else
			{
				m_pSet->MoveLast();
				pDoc->m_nRowCount = m_pSet->GetAbsolutePosition() + 2;
			}

			e->Delete();
		}
	}
}

void CDAOTestView::GetRowWidthHeight(CDC* pDC, int& nRowWidth, int& nRowHeight, int& nCharWidth)
{
	TEXTMETRIC tm;
	CDAOTestDoc* pDoc = GetDocument();
	pDC->GetTextMetrics(&tm);
	nCharWidth = tm.tmAveCharWidth + 1;
	nRowWidth = 0;
	for (int i=0;i< pDoc->m_nFileds;i++)
	{
		nRowWidth += pDoc->m_arrFiledSize[i];
	}
	nRowWidth *= nCharWidth;
	nRowHeight = tm.tmHeight;
}

int CDAOTestView::GetActiveRow()
{
	return m_nSelectedRow;
}

int CDAOTestView::GetRowCount()
{
	return GetDocument()->m_nRowCount;
}

void CDAOTestView::ChangeSelectionNextRow(BOOL bNext)
{
	if (bNext && m_nSelectedRow < min(GetRowCount() - 1, LastViewableRow() - 2))
	{
		m_nSelectedRow++;
	}
	if(!bNext && m_nSelectedRow)
	{
		m_nSelectedRow--;
	}

	UpdateRow(m_nSelectedRow);
}

void CDAOTestView::ChangeSelectionToRow(int nRow)
{
	if (nRow >= 0 && nRow < min(GetRowCount(), LastViewableRow() - 1))
	{
		m_nSelectedRow = nRow;
	}

	UpdateRow(m_nSelectedRow);
}

void CDAOTestView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
	// TODO: 在此添加专用代码和/或调用基类
	ScrollToPosition(CPoint(0,0));
	Invalidate(TRUE);
	m_nSelectedRow = -1;
	CDAOTestDoc* pDoc = GetDocument();
	if ((m_pSet = pDoc->m_pRecordset) != NULL)
	{
		UpdateScrollSizes();
	}
}

void CDAOTestView::DrawDataRow(CDC* pDC, int y)
{
	int x = 0;
	CString strTime;
	COleVariant var;
	CString str;
	CDAOTestDoc* pDoc = GetDocument();
	for (int i=0;i< pDoc->m_nFileds;i++)
	{
		var = m_pSet->GetFieldValue(i);
		switch (var.vt)
		{
		case VT_BSTR:
			str = var.bstrVal;
			break;
		case VT_I2:
			str.Format(_T("%d"), (int)var.iVal);
			break;
		case VT_I4:
			str.Format(_T("%d"), (int)var.lVal);
			break;
		case VT_R4:
			str.Format(_T("%d"), (float)var.fltVal);
			break;
		case VT_R8:
			str.Format(_T("%d"), (double)var.dblVal);
			break;
		case VT_CY:
			str = COleCurrency(var).Format();
			break;
		case VT_DATE:
			str = COleDateTime(var).Format();
			break;
		case VT_BOOL:
			str = (var.boolVal == 1) ? _T("TRUE") : _T("FALSE");
			break;
		case VT_NULL:
			str = _T("---");
			break;
		default:
			str.Format(_T("Unk Type %d\n"),var.vt);
			TRACE(_T("Unk Type %d\n"), var.vt);
			break;
		}
		pDC->TextOutW(x,y, str);
		x += pDoc->m_arrFiledSize[i] * m_nCharWidth;
	}
}


void CDAOTestView::OnSetFocus(CWnd* pOldWnd)
{
	// TODO: 在此处添加消息处理程序代码
	GetDocument()->PutQuery();
	CRowView::OnSetFocus(pOldWnd);

}


int CDAOTestView::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	SetFocus();
	return CRowView::OnMouseActivate(pDesktopWnd, nHitTest, message);
}


void CDAOTestView::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	Invalidate();
	CRowView::OnTimer(nIDEvent);
}
